package cn.bdqn.pn.controller.base;

import cn.bdqn.pn.pojo.base.City;
import cn.bdqn.pn.service.base.CityService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping("/city")
public class CityController {
    @Autowired
    private CityService cityService;

    /**
     * 页面初始化执行，查询全部省份
     * @param model
     * @param fatherId
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("/cityList")
    public String cityList(Model model,@RequestParam(value = "fatherId",defaultValue = "0") Integer fatherId,
                                 @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) throws Exception {
        System.out.println("···················执行/city/cityList···················");
        Page<City> page=cityService.listCity(new City(fatherId),pageNo,pageSize);//右侧分页城市
        List<City> provinces= cityService.listCity(new City(fatherId));//左侧所有城市
        model.addAttribute("page",page);
        model.addAttribute("provinces",provinces);
        System.out.println(JSON.toJSONString(page));
        System.out.println(JSON.toJSONString(provinces));
        return "city_list";
    }


    /**
     * 查询省/属于某省的城市/属于某城市的区并分页
     * @param pageNo 当前页数（非下标）
     * @param pageSize 页大小（默认为10）
     * @return
     */
    @RequestMapping("/hasFatherIdPage")
    @ResponseBody
    public String selectCity(@RequestParam("fatherId")Integer fatherId,@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) throws Exception {
        System.out.println("------------------------------ajax分页-----------------------------------");
        return JSONArray.toJSONString(cityService.listCity(new City(fatherId),pageNo,pageSize),true);
    }


    /**
     * 查询省/属于某省的城市/属于某城市的区 不分页
     * @return
     */
    @RequestMapping("/hasFatherId")
    @ResponseBody
    public List<City> selectCity(@Param("fatherId")Integer fatherId) throws Exception {
        System.out.println("···················执行/city/hasFatherId···················");
        List<City> cityList= cityService.listCity(new City(fatherId));
        return cityList;
    }




    /**
     * 新增城市
     * @param city
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String addCity(City city) throws Exception {
        if(Integer.parseInt(city.getMore1())!=0){//城市的值不为0就将more1的值赋给fatherId
            city.setFatherId(Integer.parseInt(city.getMore1()));
            city.setMore1(null);
        }
        City city1=null;
        if(cityService.checkRepeat(city)==null){
            city1 = cityService.addCity(city);
            if (city1 != null) {
                System.out.println("添加成功！");
            } else {
                System.out.println("添加失败！");
            }
        }
        return JSON.toJSONString(city1);
    }

    @RequestMapping("/update")
    @ResponseBody
    public boolean update(City city) throws Exception{
        System.out.println("------------------修改---------------");
        boolean flag=false;
        if(Integer.parseInt(city.getMore1())!=0){//城市的值不为0就将more1的值赋给fatherId
            city.setFatherId(Integer.parseInt(city.getMore1()));
            city.setMore1(null);
        }
        City city1=cityService.findCity(city);
        city1.setCityId(city.getCityId());
        city1.setCityName(city.getCityName());
        city1.setCityEnName(city.getCityEnName());
        city1.setCityAbbrCode(city.getCityAbbrCode());
        city1.setFatherId(city.getFatherId());
        flag=cityService.updateCity(city1);
        return flag;
    }
    /**
     * 查询城市所有记录
     * @return
     */
    @RequestMapping("/count")
    public String cityCount() throws Exception {
        return JSON.toJSONString(cityService.cityCount());
    }

    @RequestMapping("/del")
    @ResponseBody
    public String delCity(@RequestParam("cityId") Integer cityId){
        try {
            cityService.delCity(cityId);
            return JSON.toJSONString("true");
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("false");
        }
    }
    /**
     * 根据cityId查询城市
     * @param cityId
     * @return
     * @throws Exception
     */
    @RequestMapping("/find")
    @ResponseBody
    public String findCity(@RequestParam("cityId") Integer cityId) throws Exception{
        City city=new City();
        city.setCityId(cityId);
        return JSON.toJSONString(cityService.findCity(city));
    }

}
